package com.example.onlineeducation.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.example.onlineeducation.R
import com.example.onlineeducation.data.constant.ProblemType
import com.example.onlineeducation.data.entity.OptionEntity
import com.example.onlineeducation.data.entity.ProblemEntity
import com.example.onlineeducation.databinding.ProblemItemBinding
import com.example.onlineeducation.databinding.ProblemItemItemBinding
import com.example.onlineeducation.utils.YcSpannable
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageRes
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.init.YcJetpack
import com.yc.jetpacklib.recycleView.YcDecoration
import com.yc.jetpacklib.recycleView.YcViewHolder

/**
 * Creator: yc
 * Date: 2022/4/22 15:05
 * UseDes:
 */
class PractiseProblemAdapter : RecyclerView.Adapter<YcViewHolder<ProblemItemBinding>>() {
    private var mData: MutableList<ProblemEntity> = mutableListOf()
    private var mInternalAdapter: InternalAdapter? = null
    private var canSelectOptions = true

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YcViewHolder<ProblemItemBinding> {
        return YcViewHolder(ProblemItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: YcViewHolder<ProblemItemBinding>, position: Int) {
        try {
            val bindingPosition = holder.bindingAdapterPosition
            holder.viewBinding.apply {
                val item = mData[bindingPosition]
                mInternalAdapter = InternalAdapter(canSelectOptions)
                rvProblemOption.adapter = mInternalAdapter
                if (rvProblemOption.itemDecorationCount < 1)
                    rvProblemOption.addItemDecoration(YcDecoration(20))
                mInternalAdapter?.addAll(item.optionList, item.problemType)
                tvProblemContent.text = item.title
                YcSpannable.setText(
                    tvProblemContent,
                    item.title,
                    "(${ProblemType.getTypeName(item.problemType)})",
                    YcJetpack.mInstance.getResources().getColor(R.color.problem_content_type)
                )
            }
        } catch (e: Exception) {
            Log.e("ycEvery", "onBindViewHolder爆炸啦")
            e.printStackTrace()
        }
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    /**
     * 检测此题是否有选中
     * @param position Int
     * @return Boolean
     */
    fun checkProblemSelect(position: Int): Boolean {
        var isHasSelect = false
        if (position < mData.size) {
            mData[position].optionList?.apply {
                for (item in this) {
                    if (item.isSelect) {
                        isHasSelect = true
                        break
                    }
                }
            }
        }
        return isHasSelect
    }

    /*
    * 下一题
    * */
    fun next(position: Int) {
        canSelectOption()
        notifyItemChanged(position)
    }

    fun showCorrectAnswer(position: Int, answerList: List<String>?) {
        if (position < mData.size) {
            mData[position].optionList?.apply {
                if (answerList.ycIsNotEmpty()) {
                    for (item in this) {
                        item.isSelect = answerList!!.contains(item.optionId)
                    }
                } else {
                    for (item in this) {
                        item.isSelect = false
                    }
                }
            }
            canSelectOption(false)
            notifyItemChanged(position)
        }
    }

    /**
     * 选项可以被点击
     * @param position Int
     * @return Boolean
     */
    private fun canSelectOption(able: Boolean = true) {
        canSelectOptions = able
    }

    /**
     * 还原选中的Item
     * @param position Int
     */
    fun revertOption(position: Int) {
        if (position < mData.size) {
            mData[position].optionList!!.apply {
                for (item in this) {
                    item.isSelect = false
                }
            }
            canSelectOption()
            notifyItemChanged(position)
        }
    }

    fun addAll(newData: List<ProblemEntity>?) {
        mData.clear()
        if (newData.ycIsNotEmpty()) {
            mData.addAll(newData!!)
        }
        canSelectOption()
        notifyDataSetChanged()
    }


    fun getData(): List<ProblemEntity> = mData

    /*
    * canSelect：选项不能点击选择
    * */
    internal class InternalAdapter(private val canSelect: Boolean = true) : RecyclerView.Adapter<YcViewHolder<ProblemItemItemBinding>>() {
        private var mInternalData: MutableList<OptionEntity> = mutableListOf()
        private var mSingleSelectPosition: Int? = null


        @ProblemType.Type
        private var mProblemType: Int = ProblemType.SINGLE
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YcViewHolder<ProblemItemItemBinding> {
            return YcViewHolder(ProblemItemItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        }

        override fun onBindViewHolder(holder: YcViewHolder<ProblemItemItemBinding>, position: Int) {
            try {
                holder.viewBinding.apply {
                    val internalItem = mInternalData[position]
                    tvOptionContent.text = internalItem.context
                    tvOptionContent.ycSetTextColorRes(ProblemType.getTextColor(internalItem.isSelect))
                    ivOptionSelect.ycLoadImageRes(ProblemType.getDrawable(mProblemType, internalItem.isSelect))
                    clBg.setBackgroundResource(ProblemType.getBgColor(internalItem.isSelect))

                    root.setOnClickListener {
                        if (canSelect) {
                            val bindingPosition = holder.bindingAdapterPosition
                            val item = mInternalData[bindingPosition]
                            when (mProblemType) {
                                ProblemType.SINGLE -> {
                                    if (mSingleSelectPosition != bindingPosition) {
                                        if (mSingleSelectPosition != null) {
                                            mInternalData[mSingleSelectPosition!!].isSelect = false
                                            notifyItemChanged(mSingleSelectPosition!!)
                                        }
                                        mSingleSelectPosition = bindingPosition
                                        item.isSelect = true
                                        notifyItemChanged(mSingleSelectPosition!!)
                                    }
                                }
                                ProblemType.MULTIPLE -> {
                                    item.isSelect = !item.isSelect
                                    notifyItemChanged(bindingPosition)
                                }
                                else -> {
                                    if (mSingleSelectPosition != bindingPosition) {
                                        if (mSingleSelectPosition != null) {
                                            mInternalData[mSingleSelectPosition!!].isSelect = false
                                            notifyItemChanged(mSingleSelectPosition!!)
                                        }
                                        mSingleSelectPosition = bindingPosition
                                        item.isSelect = true
                                        notifyItemChanged(mSingleSelectPosition!!)
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (e: Exception) {
                Log.e("ycEvery", "onBindViewHolder爆炸啦")
                e.printStackTrace()
            }
        }

        override fun getItemCount(): Int {
            return mInternalData.size
        }

        fun addAll(newData: List<OptionEntity>?, @ProblemType.Type problemType: Int) {
            mProblemType = problemType
            mInternalData.clear()
            if (newData.ycIsNotEmpty())
                mInternalData.addAll(newData!!)
            notifyDataSetChanged()
        }
    }
}